#!/bin/bash

echo "启动 Things IoT Platform 服务..."

# 检查是否在项目根目录
if [ ! -f "things_iot_platform_solution.md" ]; then
    echo "错误：请在项目根目录运行此脚本"
    exit 1
fi

# 设置环境变量
export APP_PORT=8080
export VITE_API_BASE_URL=http://172.16.9.130:8080/api/v1

echo "环境变量已设置:"
echo "APP_PORT=$APP_PORT"
echo "VITE_API_BASE_URL=$VITE_API_BASE_URL"

# 启动后端服务
echo "启动后端服务..."
cd things_iot_backend
go run main.go &
BACKEND_PID=$!
cd ..

# 等待后端启动
echo "等待后端服务启动..."
sleep 5

# 启动前端服务
echo "启动前端服务..."
cd things_iot_frontend
npm run dev &
FRONTEND_PID=$!
cd ..

echo ""
echo "服务启动完成!"
echo "前端访问地址: http://172.16.9.130:3000"
echo "后端API地址: http://172.16.9.130:8080"
echo ""
echo "按 Ctrl+C 停止所有服务"

# 等待中断信号
trap 'kill $BACKEND_PID $FRONTEND_PID; exit' INT
wait